import wx
import CanvasHandler
import FileHandler

class MainAppHandler():
   def __init__(self):
      # Variables
      self._widgetId    = {}
      self._widgetTags  = {}
      self._rootWindows = []

      # Objects
      self.canvasHandler = CanvasHandler.CanvasHandler(self)
      self.fileHandler   = FileHandler.FileHandler(self)

      # Handler plugs
      self._handlers = {
            "FILE" : self.fileHandler.handler
      }

   def getId(self,stringId):
      if (self._widgetId.has_key(stringId)):
         return self._widgetId[stringId]
      else:
         newId = wx.NewId()
         self._widgetId[stringId] = newId
         self._widgetTags[newId]  = stringId
         return self._widgetId[stringId]

   def getTag(self,widgetId):
      return self._widgetTags[widgetId]

   def getHandle(self,widgetId):
      if type(widgetId) is not int:
         widgetId = self.getId(widgetId)
      for rootWindow in self._rootWindows:
         window = rootWindow.FindWindowById(widgetId)
         if window is not None:
            return window

   # Registers the window so that the getHandle function can search through it
   def registerWindow(self, windowObject):
      self._rootWindows.append(windowObject)

   def OnClicked(self, event):
      id = event.GetId()
      tags = self.getTag(id).split('_')
      self._handlers[tags[0]](tags[1:],event)

   # Right now canvas is the only one to use this, so it's hard coded in :p
   def OnPaint(self, event):
      self.canvasHandler.OnPaint(event)
